package cn.tedu._02notice.controller;

import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVo;
import cn.tedu._02notice.pojo.vo.NoticeListVo;
import cn.tedu._02notice.pojo.entity.Notice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/v1/notice/add")
    @ResponseBody
    public String addNotice(NoticeAddParam noticeAddParam) {
        System.out.println("添加资讯：" + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreatedTime(new Date());
        notice.setUpdatedTime(new Date());
        noticeMapper.insertNotice(notice);
        return "新增资讯成功！";
    }

    @GetMapping("/v1/notice/list")
    @ResponseBody
    public List<NoticeListVo> queryNotice(NoticeListQuery noticeListQuery) {
        System.out.println("查询资讯：" + noticeListQuery);
        List<NoticeListVo> noticeListVos = noticeMapper.noticeListQuery(noticeListQuery);
        return noticeListVos;
    }

    @GetMapping("/v1/notice/detail/{id}")
    @ResponseBody
    public NoticeDetailInfoVo queryNoticeDetailInfo(@PathVariable("id") Long id) {
        System.out.println("查询资讯详情：" + id);
        NoticeDetailInfoVo noticeDetailInfoVo = noticeMapper.queryNoticeDetailInfo(id);
        return noticeDetailInfoVo;
    }

    @GetMapping("/v1/notice/delete/{id}")
    @ResponseBody
    public String deleteNotice(@PathVariable("id") Long id) {
        System.out.println("删除资讯：" + id);
        int rows = noticeMapper.deleteNotice(id);
        return "删除资讯成功！";
    }

    @PostMapping("/v1/notice/update")
    @ResponseBody
    public String updateNotice(NoticeUpdateParam noticeUpdateParam){
        System.out.println("修改资讯：" + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        int rows = noticeMapper.updateNotice(notice);
        return "修改资讯成功！";
    }



}
